//
//  WSMusicTool.m
//  音乐与视频播放
//
//  Created by ws on 2017/6/17.
//  Copyright © 2017年 王松. All rights reserved.
//

#import "WSMusicTool.h"

#import "WSMusic.h"
#import <MediaPlayer/MediaPlayer.h>
@interface WSMusicTool()

@end
@implementation WSMusicTool
+(instancetype)ws_musicTool{
    static WSMusicTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[self alloc]init];
    });
    return tool;
}
-(void)ws_prepareToPlayWithMusic:(WSMusic *)musicModel{
   //创建一个播放器
    NSURL *musicUrl = [[NSBundle mainBundle] URLForResource:musicModel.filename withExtension:nil];
//    NSURL *musicUrl = [NSURL URLWithString:musicModel.filename];
    NSData *data = [NSData dataWithContentsOfURL:musicUrl];
    self.ws_player = [[AVAudioPlayer alloc]initWithData:data error:nil];
    //准备
    [self.ws_player prepareToPlay];
    
//    //设置锁屏音乐
//    //建议:锁屏信息最好在程序退出到后台的时候再设置 ,
//    //这里演示就在初始化音乐播放器后设置
//    //1.设置锁屏的音乐信息
//    NSMutableDictionary *info = [NSMutableDictionary dictionary];
//    //设置专辑的名称
//    info[MPMediaItemPropertyAlbumTitle] = @"中文时代";
//    //歌曲名字
//    info[MPMediaItemPropertyTitle] = musicModel.name;
//    //设置歌手
//    info[MPMediaItemPropertyArtist] = musicModel.singer;
//    //设置专辑图片(海报)
//    UIImage *image = [UIImage imageNamed:musicModel.icon];
//    MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc]initWithImage:image];
//    info[MPMediaItemPropertyArtwork] = artWork;
//    //设置时间
//    info[MPMediaItemPropertyPlaybackDuration] = @(self.ws_player.duration);
//    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;
    
    
}
-(void)ws_play{
    [self.ws_player play];
}
-(void)ws_pause{
    [self.ws_player pause];
}
@end
